package Patience;
import java.util.*;

public class Game
{
	private ArrayList<Pile> 	pilelist;
	private ArrayList<Card> 	cardlist;
	private ArrayList<Counter> 	countlist;
	
	public Game()
	{
		pilelist  = new ArrayList<Pile>();
		cardlist  = new ArrayList<Card>();
		countlist = new ArrayList<Counter>();
	}
	public void addCardsAndPile(int amount)
	{
		int i = 0;
		while(i < amount)
		{
			i++;
			addCard(i);
			Counter counter = new Counter(countlist.size()+1, 0);
			countlist.add(counter);
		}
		addPile(1, amount+1);
	}
	public void addCard(int i)
	{
		Card card = new Card(i);
		cardlist.add(card);
	}
	public void addPile(int pilenumber, int card)
	{
		Pile pile = new Pile(pilenumber, card);
		pilelist.add(pile);
	}
	public void shuffleList()
	{
		Collections.shuffle(cardlist);
	}
	/*public void test()
	{
		for(Card card : cardlist)
		{
			card.printCardnumber();
		}
		System.out.print("\n");
		for(Pile pile : pilelist)
		{
			pile.printPile();
		}
		System.out.println("amount of piles: "+pilelist.size());
		pilelist.removeAll(pilelist);
	}*/
	public void finalAmounts()
	{
		for(Counter counter : countlist)
		{
			counter.print();
		}
	}
	public void math()
	{
		for(Card card : cardlist)
		{
			mathAgain(card.getCardnumber());
		}
		for(Counter counter : countlist)
		{
			if(counter.getPileNumber() == pilelist.size())
			{
				counter.setAmount();
			}
		}
		pilelist.removeAll(pilelist);
	}
	public void mathAgain(int card)
	{
		if(possible(card))
		{
			for(Pile pile : pilelist)
			{
				if(pile.getCard() > card)
				{
					pile.setCard(card);
					return;
				}
			}
		}
		else
		{
			Pile pile = new Pile(pilelist.size()+1, card);
			pilelist.add(pile);
		}
	}
	public boolean possible(int card)
	{
		boolean possible = false;
		for(Pile pile : pilelist)
		{
			if(pile.getCard() > card)
			{
				possible = true;
			}
		}
		return possible;
	}
}
